﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BonjourQuotation.ObjectModel.ReportGenerator;
using System.Xml.Serialization;
using System.IO;
using BonjourQuotation.UI.Models.ReportModel;

namespace BonjourQuotation.UI.GeneratedReport
{
    public class PageProprties
    {
        public string PageWidth { get; set; }
        public string PageHeight { get; set; }
        public string BodyWidht { get; set; }
        public string BodyHeight { get; set; }
    }
    public class VendorGroupReportGenerator
    {
        private List<string> m_allFields;
        private List<string> m_selectedFields;
        public VendorGroupReportOtherViewModel OtherData { get; set; }
        public string TabName { get; set; }
        public string PaperType { get; set; }
        public List<string> AllFields
        {
            get { return m_allFields; }
            set { m_allFields = value; }
        }

        public List<string> SelectedFields
        {
            get { return m_selectedFields; }
            set { m_selectedFields = value; }
        }
        public void WriteXml(Stream stream)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(Rdl.Report));
            serializer.Serialize(stream, CreateReport());
        }

        private Rdl.Report CreateReport()
        {
            Rdl.Report report = new Rdl.Report();
            
            report.Items = new object[] 
                {
                    VndGrpRepData.PaperProprties[PaperType].PageWidth,
                    VndGrpRepData.PaperProprties[PaperType].PageHeight,
                    CreatePageHeader(),
                    CreateDataSources(),
                    CreateBody(),
                    CreateDataSets(),
                    VndGrpRepData.PaperProprties[PaperType].BodyWidht,
                    CreatePageFooter(),
                    "1cm",
                    "1cm",
                    "1cm",
                    "1.5cm",
                };
            report.ItemsElementName = new Rdl.ItemsChoiceType37[]
                { 
                    Rdl.ItemsChoiceType37.PageWidth,
                    Rdl.ItemsChoiceType37.PageHeight,
                    Rdl.ItemsChoiceType37.PageHeader,
                    Rdl.ItemsChoiceType37.DataSources, 
                    Rdl.ItemsChoiceType37.Body,
                    Rdl.ItemsChoiceType37.DataSets,
                    Rdl.ItemsChoiceType37.Width,
                    Rdl.ItemsChoiceType37.PageFooter,
                    Rdl.ItemsChoiceType37.BottomMargin,
                    Rdl.ItemsChoiceType37.TopMargin,
                    Rdl.ItemsChoiceType37.RightMargin,
                    Rdl.ItemsChoiceType37.LeftMargin,
                };
            return report;
        }

        private Rdl.DataSourcesType CreateDataSources()
        {
            Rdl.DataSourcesType dataSources = new Rdl.DataSourcesType();
            dataSources.DataSource = new Rdl.DataSourceType[] { CreateDataSource() };
            return dataSources;
        }

        private Rdl.DataSourceType CreateDataSource()
        {
            Rdl.DataSourceType dataSource = new Rdl.DataSourceType();
            dataSource.Name = "DummyDataSource";
            dataSource.Items = new object[] { CreateConnectionProperties() };
            return dataSource;
        }

        private Rdl.ConnectionPropertiesType CreateConnectionProperties()
        {
            Rdl.ConnectionPropertiesType connectionProperties = new Rdl.ConnectionPropertiesType();
            connectionProperties.Items = new object[]
                {
                    "",
                    "SQL",
                };
            connectionProperties.ItemsElementName = new Rdl.ItemsChoiceType[]
                {
                    Rdl.ItemsChoiceType.ConnectString,
                    Rdl.ItemsChoiceType.DataProvider,
                };
            return connectionProperties;
        }

        private Rdl.BodyType CreateBody()
        {
            Rdl.BodyType body = new Rdl.BodyType();
            body.Items = new object[]
                {
                    CreateReportItems(),
                    VndGrpRepData.PaperProprties[PaperType].BodyHeight,
                };
            body.ItemsElementName = new Rdl.ItemsChoiceType30[]
                {
                    Rdl.ItemsChoiceType30.ReportItems,
                    Rdl.ItemsChoiceType30.Height,
                };
            return body;
        }

        private Rdl.ReportItemsType CreateReportItems()
        {
            Rdl.ReportItemsType reportItems = new Rdl.ReportItemsType();
            TableRdlGenerator tableGen = new TableRdlGenerator();
            tableGen.Fields = m_selectedFields;
            tableGen.HeaderProprites = VndGrpRepData.TabColumns[TabName];
            reportItems.Items = new object[] { tableGen.CreateTable(), CreateVendor(), CreateTotalLine(), CreateReportTotal() };
            return reportItems;
        }

        private Rdl.DataSetsType CreateDataSets()
        {
            Rdl.DataSetsType dataSets = new Rdl.DataSetsType();
            dataSets.DataSet = new Rdl.DataSetType[] { CreateDataSet() };
            return dataSets;
        }

        private Rdl.DataSetType CreateDataSet()
        {
            Rdl.DataSetType dataSet = new Rdl.DataSetType();
            dataSet.Name = "MyData";
            dataSet.Items = new object[] { CreateQuery(), CreateFields() };
            return dataSet;
        }

        private Rdl.QueryType CreateQuery()
        {
            Rdl.QueryType query = new Rdl.QueryType();
            query.Items = new object[] 
                {
                    "DummyDataSource",
                    "",
                };
            query.ItemsElementName = new Rdl.ItemsChoiceType2[]
                {
                    Rdl.ItemsChoiceType2.DataSourceName,
                    Rdl.ItemsChoiceType2.CommandText,
                };
            return query;
        }

        private Rdl.FieldsType CreateFields()
        {
            Rdl.FieldsType fields = new Rdl.FieldsType();

            fields.Field = new Rdl.FieldType[m_allFields.Count];
            for (int i = 0; i < m_allFields.Count; i++)
            {
                fields.Field[i] = CreateField(m_allFields[i]);
            }

            return fields;
        }

        private Rdl.FieldType CreateField(String fieldName)
        {
            Rdl.FieldType field = new Rdl.FieldType();
            field.Name = fieldName;
            field.Items = new object[] { fieldName };
            field.ItemsElementName = new Rdl.ItemsChoiceType1[] { Rdl.ItemsChoiceType1.DataField };
            return field;
        }
        private Rdl.PageHeaderFooterType CreatePageFooter()
        {
            Rdl.PageHeaderFooterType footer = new Rdl.PageHeaderFooterType();

            Rdl.TextboxType DateTimeNow = new Rdl.TextboxType();
            DateTimeNow.Name = "DateTimeNow";
            DateTimeNow.Items = new object[] 
                {
                    DateTime.Now.ToString("dddd, MMMM dd, yyyy"),
                    true,
                    "0.26458cm",
                    "0.15494cm",
                    "8.37375cm",
                    "0.6cm",
                };
            DateTimeNow.ItemsElementName = new Rdl.ItemsChoiceType14[] 
                {
                    Rdl.ItemsChoiceType14.Value,
                    Rdl.ItemsChoiceType14.CanGrow,
                    Rdl.ItemsChoiceType14.Left,
                    Rdl.ItemsChoiceType14.Top,
                    Rdl.ItemsChoiceType14.Width,
                    Rdl.ItemsChoiceType14.Height,
                };
            Rdl.TextboxType PagesFromTo = new Rdl.TextboxType();
            PagesFromTo.Name = "PagesFromTo";
            PagesFromTo.Items = new object[] 
                {
                    "=\" Page \" & Globals.PageNumber & \" of \" & Globals.TotalPages",
                    true,
                    "15.61042cm",
                    "0.10583cm",
                    "2.47353cm",
                    "0.6cm",
                };
            PagesFromTo.ItemsElementName = new Rdl.ItemsChoiceType14[] 
                {
                    Rdl.ItemsChoiceType14.Value,
                    Rdl.ItemsChoiceType14.CanGrow,
                    Rdl.ItemsChoiceType14.Left,
                    Rdl.ItemsChoiceType14.Top,
                    Rdl.ItemsChoiceType14.Width,
                    Rdl.ItemsChoiceType14.Height,
                };

            Rdl.ReportItemsType FooterItems = new Rdl.ReportItemsType();
            FooterItems.Items = new object[] { DateTimeNow, PagesFromTo };


            footer.Items = new object[]
                {
                    FooterItems,
                    "1cm",
                    true,
                    true,
                };
            footer.ItemsElementName = new Rdl.ItemsChoiceType34[]
                { 
                    Rdl.ItemsChoiceType34.ReportItems,
                    Rdl.ItemsChoiceType34.Height,
                    Rdl.ItemsChoiceType34.PrintOnFirstPage,
                    Rdl.ItemsChoiceType34.PrintOnLastPage,
                };
            return footer;
        }
        private Rdl.PageHeaderFooterType CreatePageHeader()
        {
            Rdl.PageHeaderFooterType Header = new Rdl.PageHeaderFooterType();

            Rdl.LineType HeaderLineTop = new Rdl.LineType();
            HeaderLineTop.Name = "HeaderLineTop";
            HeaderLineTop.Items = new object[] 
                {
                    CreateReportTotalStyle(),
                    "0cm",
                    "0.2cm",
                    "18.38219cm",
                    "0cm",
                };
            HeaderLineTop.ItemsElementName = new Rdl.ItemsChoiceType11[] 
                {
                    Rdl.ItemsChoiceType11.Style,
                    Rdl.ItemsChoiceType11.Left,
                    Rdl.ItemsChoiceType11.Top,
                    Rdl.ItemsChoiceType11.Width,
                    Rdl.ItemsChoiceType11.Height,
                };

            Rdl.LineType HeaderLineBottom = new Rdl.LineType();
            HeaderLineBottom.Name = "HeaderLineBottom";
            HeaderLineBottom.Items = new object[] 
                {
                    CreateReportTotalStyle(),
                    "0cm",
                    "1.8cm",
                    "18.38219cm",
                    "0cm",
                };
            HeaderLineBottom.ItemsElementName = new Rdl.ItemsChoiceType11[] 
                {
                    Rdl.ItemsChoiceType11.Style,
                    Rdl.ItemsChoiceType11.Left,
                    Rdl.ItemsChoiceType11.Top,
                    Rdl.ItemsChoiceType11.Width,
                    Rdl.ItemsChoiceType11.Height,
                };
            Rdl.TextboxType MainTitle = new Rdl.TextboxType();
            MainTitle.Name = "MainTitle";
            MainTitle.Items = new object[] 
                {
                    CreateTitleStyle(),
                    "Vendor-Group Report",
                    true,
                    "0.00317cm",
                    "0.58526cm",
                    "9.95344cm",
                    "0.97292cm",
                };
            MainTitle.ItemsElementName = new Rdl.ItemsChoiceType14[] 
                {
                    Rdl.ItemsChoiceType14.Style,
                    Rdl.ItemsChoiceType14.Value,
                    Rdl.ItemsChoiceType14.CanGrow,
                    Rdl.ItemsChoiceType14.Left,
                    Rdl.ItemsChoiceType14.Top,
                    Rdl.ItemsChoiceType14.Width,
                    Rdl.ItemsChoiceType14.Height,
                };
            Rdl.ReportItemsType HeaderItems = new Rdl.ReportItemsType();
            HeaderItems.Items = new object[] { MainTitle, HeaderLineTop, HeaderLineBottom };
            Header.Items = new object[]
                {
                    HeaderItems,
                    "2cm",
                    true,
                    true,
                };
            Header.ItemsElementName = new Rdl.ItemsChoiceType34[]
                { 
                    Rdl.ItemsChoiceType34.ReportItems,
                    Rdl.ItemsChoiceType34.Height,
                    Rdl.ItemsChoiceType34.PrintOnFirstPage,
                    Rdl.ItemsChoiceType34.PrintOnLastPage,
                };
            return Header;
        }
        private Rdl.StyleType CreateTitleStyle()
        {
            Rdl.StyleType style = new Rdl.StyleType();
            style.Items = new object[] 
                {
                    "24pt",
                    "Left",
                };
            style.ItemsElementName = new Rdl.ItemsChoiceType5[] 
                {
                    Rdl.ItemsChoiceType5.FontSize,
                    Rdl.ItemsChoiceType5.TextAlign,
                };
            return style;
        }
        private Rdl.StyleType CreateTotalLineStyle()
        {
            Rdl.StyleType style = new Rdl.StyleType();
            style.Items = new object[] 
                {
                    "2pt",
                    "2pt",
                    "2pt",
                    "2pt",
                };
            style.ItemsElementName = new Rdl.ItemsChoiceType5[] 
                {
                    Rdl.ItemsChoiceType5.PaddingBottom,
                    Rdl.ItemsChoiceType5.PaddingLeft,
                    Rdl.ItemsChoiceType5.PaddingRight,
                    Rdl.ItemsChoiceType5.PaddingTop,
                };
            return style;
        }
        private Rdl.LineType CreateTotalLine()
        {
            Rdl.LineType TotalLine = new Rdl.LineType();
            TotalLine.Name = "TotalLine";
            TotalLine.Items = new object[] 
                {
                    CreateReportTotalStyle(),
                    "0cm",
                    "2.3cm",
                    "18.38219cm",
                    "0cm",
                };
            TotalLine.ItemsElementName = new Rdl.ItemsChoiceType11[] 
                {
                    Rdl.ItemsChoiceType11.Style,
                    Rdl.ItemsChoiceType11.Left,
                    Rdl.ItemsChoiceType11.Top,
                    Rdl.ItemsChoiceType11.Width,
                    Rdl.ItemsChoiceType11.Height,
                };
            return TotalLine;
        }
        private Rdl.StyleType CreateReportTotalStyle()
        {
            Rdl.StyleType style = new Rdl.StyleType();
            style.Items = new object[] 
                {
                    "11pt",
                    "Bold",
                    "Left",
                    "2pt",
                    "2pt",
                    "2pt",
                    "2pt",
                };
            style.ItemsElementName = new Rdl.ItemsChoiceType5[] 
                {
                    Rdl.ItemsChoiceType5.FontSize,
                    Rdl.ItemsChoiceType5.FontWeight,
                    Rdl.ItemsChoiceType5.TextAlign,
                    Rdl.ItemsChoiceType5.PaddingBottom,
                    Rdl.ItemsChoiceType5.PaddingLeft,
                    Rdl.ItemsChoiceType5.PaddingRight,
                    Rdl.ItemsChoiceType5.PaddingTop,
                };
            return style;
        }
        private Rdl.TextboxType CreateReportTotal()
        {
            Rdl.TextboxType ReportTotal = new Rdl.TextboxType();
            ReportTotal.Name = "ReportTotal";
            ReportTotal.Items = new object[] 
                {
                    CreateReportTotalStyle(),
                    OtherData.ReportTotal,
                    true,
                    "0cm",
                    "2.5cm",
                    "18.38219cm",
                    " 0.6cm",
                };
            ReportTotal.ItemsElementName = new Rdl.ItemsChoiceType14[] 
                {
                    Rdl.ItemsChoiceType14.Style,
                    Rdl.ItemsChoiceType14.Value,
                    Rdl.ItemsChoiceType14.CanGrow,
                    Rdl.ItemsChoiceType14.Left,
                    Rdl.ItemsChoiceType14.Top,
                    Rdl.ItemsChoiceType14.Width,
                    Rdl.ItemsChoiceType14.Height,
                };
            return ReportTotal;
        }
        private Rdl.StyleType CreateVendorStyle()
        {
            Rdl.StyleType style = new Rdl.StyleType();
            style.Items = new object[] 
                {
                    CreateVendorBorderStyle(),
                    "12pt",
                    "Bold",
                    "Left",
                    "2pt",
                    "2pt",
                    "2pt",
                    "2pt",
                };
            style.ItemsElementName = new Rdl.ItemsChoiceType5[] 
                {
                    Rdl.ItemsChoiceType5.BorderStyle,
                    Rdl.ItemsChoiceType5.FontSize,
                    Rdl.ItemsChoiceType5.FontWeight,
                    Rdl.ItemsChoiceType5.TextAlign,
                    Rdl.ItemsChoiceType5.PaddingBottom,
                    Rdl.ItemsChoiceType5.PaddingLeft,
                    Rdl.ItemsChoiceType5.PaddingRight,
                    Rdl.ItemsChoiceType5.PaddingTop,
                };
            return style;
        }
        private Rdl.BorderColorStyleWidthType CreateVendorBorderStyle()
        {
            Rdl.BorderColorStyleWidthType borderstyle = new Rdl.BorderColorStyleWidthType();
            borderstyle.Items = new object[] 
                {
                    "Solid",
                };
            borderstyle.ItemsElementName = new Rdl.ItemsChoiceType3[] 
                {
                    Rdl.ItemsChoiceType3.Default,
                };
            return borderstyle;
        }
        private Rdl.TextboxType CreateVendor()
        {
            Rdl.TextboxType Vendor = new Rdl.TextboxType();
            Vendor.Name = "Vendor";
            Vendor.Items = new object[] 
                {
                    CreateVendorStyle(),
                    OtherData.Vendor,
                    true,
                    "0.00318cm",
                    "0cm",
                    "18.38218cm",
                    " 0.6cm",
                };
            Vendor.ItemsElementName = new Rdl.ItemsChoiceType14[] 
                {
                    Rdl.ItemsChoiceType14.Style,
                    Rdl.ItemsChoiceType14.Value,
                    Rdl.ItemsChoiceType14.CanGrow,
                    Rdl.ItemsChoiceType14.Left,
                    Rdl.ItemsChoiceType14.Top,
                    Rdl.ItemsChoiceType14.Width,
                    Rdl.ItemsChoiceType14.Height,
                };
            return Vendor;
        }
    }
}